setwd("")
rm(list=ls())
library(ggplot2)

data <- read.csv("../lgbt_cace_results.csv", stringsAsFactors = FALSE)
var.names <- read.csv("../lgbt_var_names.csv", stringsAsFactors = FALSE)

make.cace <- function(dv) {
  # Pick which variable to plot
  data <- subset(data, data$DV == dv)
  title <- var.names[var.names$dv == dv, "caption"]
  
  # Get overall results
  all.respondents <- data[data$Exposures == "All respondents", "Estimate"]
  any.compliers <- data[data$Exposures == "Any compliers", "Estimate"]
  data <- subset(data, !(data$Exposures %in% c("All respondents", "Any compliers")))
  data$Exposures <- as.numeric(data$Exposures)
  
  plot <- ggplot(data, aes(x = Exposures, y = Estimate, size = 1/SE^2)) + 
    geom_point(shape = 21) +
    geom_smooth(method = "lm", mapping = aes(weight = 1/SE^2), 
                color = "black") +
    geom_hline(aes(yintercept = all.respondents, linetype="All respondents"), color = "red") +
    geom_hline(aes(yintercept = any.compliers, linetype="Saw ad at least once"), color = "blue") +
    scale_linetype_manual(name = NULL, values = c("dashed", "solid"), 
                          guide = guide_legend(override.aes = list(color = c("red", "blue")))) +
    labs(title = paste0("Effect on ", title), caption = "Note: Fitted line weighted by the precision of each observation.") + 
    theme_bw() +
    theme(legend.position = "bottom") +
    scale_size(guide=FALSE)
  ggsave(paste(dv, ".pdf"), plot)
}

for (i in 1:nrow(var.names)) {
  make.cace(var.names[i, "dv"])
}